package com.sunriz.motordrive.server.photoVoltaic.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sunriz.motordrive.domain.DAO.PVNameDAO;
import com.sunriz.motordrive.domain.DAO.PvHcLogDAO;
import com.sunriz.motordrive.domain.DTO.PvHcLogsDTO;
import com.sunriz.motordrive.domain.DTO.Result;
import com.sunriz.motordrive.domain.POJO.photoVolataic.DeviceIdAndValuePOJO;
import com.sunriz.motordrive.mapper.PvHcLogMapper;
import com.sunriz.motordrive.server.photoVoltaic.LogInfoServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * ClassName: LogInfoServerImpl
 * Package: com.sunriz.motordrive.server.photoVoltaic.Impl
 * Description:
 *
 * @Author BuTang
 * @Create 2024/9/2 10:34
 * @Version 1.0
 */
@Service
public class LogInfoServerImpl implements LogInfoServer {

    @Autowired
    PvHcLogMapper pvHcLogMapper;

    @Override
    public Result getLog(String startTime, String endTime, List<Integer>  deviceId , String infoName) {

        // 格式化时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime startTime1 = LocalDateTime.parse(startTime, formatter);
        LocalDateTime endTime1 = LocalDateTime.parse(endTime, formatter);
        // 格式化为没有秒的字符串
        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
//        String formattedDateTime = dateTime.format(outputFormatter);


        // 返回数据
        PvHcLogsDTO result = new PvHcLogsDTO();
        List<DeviceIdAndValuePOJO> resultDevice = new ArrayList();
        result.device = resultDevice;
        result.infoName = infoName;

        // 初始化返回值，添加对应数量的device
        deviceId.forEach(id -> {
            DeviceIdAndValuePOJO idAndValue = new DeviceIdAndValuePOJO(id, new LinkedHashMap<>());
            result.device.add(idAndValue);
        });

        // 获取范围内的数据，并进行处理

        for (int i = 0; i < deviceId.size(); i++) {
            List<PvHcLogDAO> logsByDeviceIdAndTime = pvHcLogMapper.getLogsByDeviceIdAndTime(startTime1, endTime1, deviceId.get(i));

            int index = i ;

            // 将value添加到返回值中
            logsByDeviceIdAndTime.forEach(pvHcLogVo -> {
                pvHcLogVo.getInfo();
                JSONObject jsonObject = JSON.parseObject(pvHcLogVo.getInfo());
                String formatTime = pvHcLogVo.time.format(outputFormatter);

                int value1;

                boolean b = jsonObject.containsKey(infoName);
                if (b) {
                    String valueString = jsonObject.getString(infoName);
                    value1 = (int) Double.parseDouble(valueString);
                    result.getDevice().get(index).data.put(formatTime, value1);
                } else {
                    result.getDevice().get(index).data.put(formatTime, null);
                }
            });
        }



        return Result.success(result);
    }

    /**
     * 获取deviceId的list列表
     * @return list列表
     */
    @Override
    public Result logInfoServer() {
        List<Integer> deviceInfo = pvHcLogMapper.getDeviceInfo();
        return Result.success(deviceInfo);
    }

    @Override
    public Result getNames() {
        List<PVNameDAO> pvNames = pvHcLogMapper.getPVNames();
        return Result.success(pvNames);
    }
}
